package com.shopping.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.shopping.bean.ShopCar;
import com.shopping.service.ShopCarService;
import com.shopping.service.UserService;
import com.shopping.service.impl.ShopCarServiceImpl;
import com.shopping.service.impl.UserServiceImpl;


/**
 * @author zjh
 *
 */
@WebServlet("/getGoodsNum.do")
public class GetShopCarGoodsNumServlet extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		UserService us = new UserServiceImpl();
		ShopCarService scs = new ShopCarServiceImpl();
		int userId = 0;
		try {
			userId = us.getUserId(username, password);
		} catch (Exception e) {
			e.printStackTrace();
		}
		List<ShopCar> list = scs.getShopCar(userId);
		ObjectMapper om = new ObjectMapper();
		response.setCharacterEncoding("UTF-8"); //设置 后端给前端传回的编码格式为utf-8
		response.setContentType("application/json");// 后端告知前端 返回的数据 是 json格式(不是html /css)
		response.getWriter().print(om.writeValueAsString(list));
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
